package com.log4jviewer.logfile.fields;

import java.text.ParseException;

/**
 * Class constructs the whole Regex for log's field.
 * 
 * @author <a href="mailto:rd.ryly@gmail.com">Ruslan Diachenko</a>
 */
public abstract class AbstractField {

    private static final String ALIGN_PADDING_REGEX = "[ ]*";

    private boolean leftAlign;

    private boolean rightAlign;

    private LogFieldName logFieldName;

    public abstract String getRawRegex();

    public AbstractField(final LogFieldName logFieldName, final boolean leftAlign, final boolean rightAlign) {
        this.leftAlign = leftAlign;
        this.rightAlign = rightAlign;
        this.logFieldName = logFieldName;
    }

    public LogFieldName getLogFieldName() {
        return logFieldName;
    }

    public Object decodeValue(final String value) throws ParseException {
        return value;
    }

    public String getRegex() {
        String regex = "";

        if (leftAlign) {
            regex = "(" + getRawRegex() + ALIGN_PADDING_REGEX + ")";
        } else if (rightAlign) {
            regex = "(" + ALIGN_PADDING_REGEX + getRawRegex() + ")";
        } else {
            regex = "(" + getRawRegex() + ")";
        }
        return regex;
    }
}
